﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class Objects
        Inherits clsBase
#Region "Private Variables"
        Private _Id As Integer
        Private _Name As String
        Private _Photo As String
        Private _Description As String
        Private _UserEmail As String
        Private _IsGift As Boolean
        Private _Created As DateTime
        Private _Modified As DateTime
#End Region
#Region "Properties"
        Public Property Id As Integer
            Set(value As Integer)
                _Id = value
            End Set
            Get
                Return _Id
            End Get
        End Property
        Public Property Name As String
            Set(value As String)
                _Name = value
            End Set
            Get
                Return _Name
            End Get
        End Property
        Public Property Photo As String
            Set(value As String)
                _Photo = value
            End Set
            Get
                Return _Photo
            End Get
        End Property
        Public Property Description As String
            Set(value As String)
                _Description = value
            End Set
            Get
                Return _Description
            End Get
        End Property
        Public Property UserEmail As String
            Set(value As String)
                _UserEmail = value
            End Set
            Get
                Return _UserEmail
            End Get
        End Property
        Public Property IsGift As Boolean
            Set(value As Boolean)
                _IsGift = value
            End Set
            Get
                Return _IsGift
            End Get
        End Property
        Public Property Created As DateTime
            Set(value As DateTime)
                _Created = value
            End Set
            Get
                Return _Created
            End Get
        End Property
        Public Property Modified As DateTime
            Set(value As DateTime)
                _Modified = value
            End Set
            Get
                Return _Modified
            End Get
        End Property
#End Region
#Region "Constructor"
        Public Sub New( ByVal Id_ As Integer, ByVal Name_ As String, ByVal Photo_ As String, ByVal Description_ As String, ByVal UserEmail_ As String, ByVal IsGift_ As Boolean, ByVal Created_ As DateTime, ByVal Modified_ As DateTime)
            Me.Id = Id_
            Me.Name = Name_
            Me.Photo = Photo_
            Me.Description = Description_
            Me.UserEmail = UserEmail_
            Me.IsGift = IsGift_
            Me.Created = Created_
            Me.Modified = Modified_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetObjectsFromReader(ByVal reader_ As IDataReader) As Objects
            Dim rtrn As Objects = New Objects(reader_("Id"), reader_("Name"), reader_("Photo"), reader_("Description"), reader_("UserEmail"), reader_("IsGift"), reader_("Created"), reader_("Modified"))
            Return rtrn
        End Function
        Public Shared Function GetObjectById(ByVal Id_ As Integer) As Objects
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Objects where id=@Id"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text

                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetObjectsFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Private Shared Function GetObjectsListFromReader(ByVal reader As IDataReader) As List(Of Objects)
            Dim rtrnList As New List(Of Objects)
            Do While reader.Read
                rtrnList.Add(GetObjectsFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetObjectsListByFilter(ByVal Filtre_ As String, ByVal FiltreOrder_ As String) As List(Of Objects)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Objects where " & Filtre_ & "' order by " & FiltreOrder_ & " asc"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetObjectsListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertObject(ByVal Name_ As String, ByVal Photo_ As String, ByVal Description_ As String, ByVal UserEmail_ As String, ByVal IsGift_ As Boolean) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Objects(name,photo,description,user_email,is_gift,created,modified) values(@Name,@Photo,@Description,@UserEmail,@IsGift,@created,@modified)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name_
                cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = Photo_
                cmd.Parameters.Add("@Description", SqlDbType.Text).Value = Description_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@IsGift", SqlDbType.Bit).Value = IsGift_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Now.Date
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function UpdateObject(ByVal Id_ As Integer, ByVal Name_ As String, ByVal Photo_ As String, ByVal Description_ As String, ByVal UserEmail_ As String, ByVal IsGift_ As Boolean) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Objects set Name=@Name, photo=@Photo, description=@Description, user_email=@UserEmail, is_gift=@IsGift, modified=@modified where id=@Id"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name_
                cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = Photo_
                cmd.Parameters.Add("@Description", SqlDbType.Text).Value = Description_
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@IsGift", SqlDbType.Bit).Value = IsGift_
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteObject(ByVal Id_ As Integer) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Objects Where id=@Id"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Objects Where id=@Id"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region

    End Class
End Namespace


